package cn.ryh.redisCluster;

import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName redisCluster
 * @Description 连接redis集群
 * @Author RaoYH
 * @Date 2018/10/30 8:56
 */
public class redisCluster {
    @Test
  public void connectRedisCluster() throws Exception{
      //创建一个JedisCluster对象
      Set<HostAndPort> nodes = new HashSet<>();
      nodes.add(new HostAndPort("192.168.10.117", 7001));
      nodes.add(new HostAndPort("192.168.10.117", 7002));
      nodes.add(new HostAndPort("192.168.10.117", 7003));
      nodes.add(new HostAndPort("192.168.10.117", 7004));
      nodes.add(new HostAndPort("192.168.10.117", 7005));
      nodes.add(new HostAndPort("192.168.10.117", 7006));
      //在nodes中指定每个节点的地址
      //jedisCluster在系统中是单例的。
      JedisCluster jedisCluster = new JedisCluster(nodes);
      jedisCluster.set("name", "zhangsan");
      jedisCluster.set("value", "100");
      String name = jedisCluster.get("name");
      String value = jedisCluster.get("value");
      System.out.println(name);
      System.out.println(value);
      //系统关闭时关闭jedisCluster
      jedisCluster.close();
  }

}
